package cz.data.domain.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.system.mapstruct.AuthorizeClientMapper;
import cz.data.domain.system.model.dto.AuthorizeClientDto;
import cz.data.domain.system.model.entity.AuthorizeClient;
import cz.data.domain.system.model.vo.AuthorizeClientVo;
import cz.data.domain.system.service.AuthorizeClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"授权客户端"})
@RestController
public class AuthorizeClientController {

    @Resource
    AuthorizeClientService service;
    @Resource
    AuthorizeClientMapper mapper;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "授权客户端-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/authorize/client/{id}")
    public SimpleDataResponse<AuthorizeClientVo> getAuthorizeClientById(@PathVariable String id) {
        AuthorizeClient authorizeClient = service.getById(id);
        return new SimpleDataResponse<>(mapper.toVO(authorizeClient));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "授权客户端-分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientName", value = "客户端名称", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "分页页数", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页页大小", dataType = "Integer")
    })
    @GetMapping("/authorize/client/page")
    public SimpleDataResponse<JsonPage<AuthorizeClientVo>> getAuthorizeClientPage(
            @RequestParam(value = "clientName", required = false) String clientName,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        QueryWrapper<AuthorizeClient> wrapper = new QueryWrapper<>();
        wrapper.lambda().likeRight(StringUtils.isNotBlank(clientName), AuthorizeClient::getClientName, clientName);
        IPage<AuthorizeClient> page = service.page(new Page<>(pageNum, pageSize), wrapper);
        List<AuthorizeClientVo> collect = page.getRecords().stream().map(mapper::toVO).collect(Collectors.toList());
        JsonPage<AuthorizeClientVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "授权客户端-新增")
    @ApiImplicitParam(name = "request", value = "详细实体request", required = true, dataType = "AuthorizeClientDto")
    @PostMapping("/authorize/client")
    public SimpleDataResponse<AuthorizeClientVo> saveAuthorizeClient(
            @RequestBody @Validated({ValidationGroups.Insert.class}) AuthorizeClientDto request) {
        AuthorizeClient authorizeClient = service.saveAuthorizeClient(request);
        return new SimpleDataResponse<>(mapper.toVO(authorizeClient));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "授权客户端-修改")
    @ApiImplicitParam(name = "request", value = "详细实体request", required = true, dataType = "AuthorizeClientDto")
    @PutMapping("/authorize/client")
    public SimpleDataResponse<AuthorizeClientVo> updateAuthorizeClient(
            @RequestBody @Validated({ValidationGroups.Update.class}) AuthorizeClientDto request) {
        AuthorizeClient authorizeClient = service.updateAuthorizeClient(request);
        return new SimpleDataResponse<>(mapper.toVO(authorizeClient));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "授权客户端-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/authorize/client/{id}")
    public BaseResponse deleteAuthorizeClientById(@PathVariable String id) {
        return BaseResponse.withBoolean(service.removeById(id));
    }
}
